<?php
declare (strict_types = 1);
namespace app\controller;
use think\exception\ValidateException;
use think\Request;
use app\model\Admin as AdminModel;
use app\validate\User as UserValidate;
use app\middleware\Auth as AuthMiddleware;

class Admin
{
    protected $middleware = [AuthMiddleware::class];

    /**
     * 提示模板路径
     *
     * @var string
     */
    private $toast = 'public/toast';

    /**
     * 显示资源列表
     *
     * @return string
     */
    public function index()
    {
        return view('index', [
            'list'  =>   AdminModel::withSearch(['id', 'username',  'create_at'], [
                'id'        =>  request()->param('id'),
                'username'      =>  request()->param('username'),
                'create_at'   =>  request()->param('create_at')
            ])->paginate([
                'list_rows'     =>  5,
                'query'         =>  request()->param()
            ]),
            'orderTime'         =>  request()->param('create_time') == 'desc' ? 'asc' : 'desc'
        ]);
    }

    /**
     * 显示创建资源表单页.
     *
     * @return \think\Response
     */
    public function create()
    {
        return view('create');
    }

    /**
     * 保存新建的资源
     *
     * @param  \think\Request  $request
     * @return \think\Response
     */
    public function save(Request $request)
    {
        $data = $request->param();

        try {
            validate(UserValidate::class)->batch(true)->check($data);
        } catch (ValidateException $exception) {
            return view($this->toast, [
                'infos'     =>  $exception->getError(),
                'url_text'  =>  '返回上一页',
                'url_path'  =>  url('/admin/create')
            ]);
        }

        //密码加密
        $data['password'] = sha1($data['password']);
        //写入数据库
        $id = AdminModel::create($data)->getData('id');


        //返回
        return $id ? view($this->toast, [
            'infos'     =>  ['恭喜，注册成功~'],
            'url_text'  =>  '去首页',
            'url_path'  =>  url('/')
        ]) : '注册失败';
    }

    /**
     * 显示指定的资源
     *
     * @param  int  $id
     * @return \think\Response
     */
    public function read($id)
    {
        //
    }

    /**
     * 显示编辑资源表单页.
     *
     * @param  int  $id
     * @return \think\Response
     */
    public function edit($id)
    {
        return view('edit', [
            'obj'   =>  AdminModel::find($id)
        ]);
    }

    /**
     * 保存更新的资源
     *
     * @param  \think\Request  $request
     * @param  int  $id
     * @return string
     */
    public function update(Request $request, $id)
    {
        $data = $request->param();

        try {
            validate(UserValidate::class)
                        ->scene('edit')
                        ->batch(true)
                        ->check($data);
        } catch (ValidateException $exception) {
            return view($this->toast, [
                'infos'     =>  $exception->getError(),
                'url_text'  =>  '返回上一页',
                'url_path'  =>  url('/admin/'.$id.'/edit')
            ]);
        }
        //如有密码，则写入
        if (!empty($data['newpasswordnot'])) {
            $data['password'] = md5(sha1($data['newpasswordnot']));
        }
        return AdminModel::update($data) ? view($this->toast, [
            'infos'     =>  ['恭喜，修改成功~'],
            'url_text'  =>  '返回',
            'url_path'  =>  url('/admin')
        ]) : '修改失败';
    }

    /**
     * 删除指定资源
     *
     * @param  int  $id
     * @return string
     */
    public function delete($id)
    {
        return UserModel::destroy($id) ? view($this->toast, [
            'infos'     =>  ['恭喜，删除成功~'],
            'url_text'  =>  '去首页',
            'url_path'  =>  url('/admin')
        ]) : '删除失败！';
    }
}
